#pragma once
#include <string>
#include <ctime>
#include <pthread.h>

static int num = 1;
typedef void (*callback)();
//模板传参数
class Thread
{
public:
    Thread(callback cb)
    :_tid(0), _name(""), _time(0), _running(false), _cb(cb)
    {}

    static void* routine(void* args)
    {
        Thread *tp = static_cast<Thread *>(args);
        tp->entry();
        return nullptr;
    }

    void run()
    {
        _name = "thread-" + std::to_string(num++);
        _time = time();
        _running = true;
        pthread_create(&_tid, nullptr, routine, this);
    }

    void join()
    {
        pthread_join(_tid, nullptr);
    }
    //传入参数
    void entry()
    {
        _cb();
    }

    std::string name()
    {
        return _name;
    }

    pthread_t gettid()
    {
        return _tid;
    }

    uint64_t time()
    {
        return _time;
    }

    bool isrunning()
    {
        return _running;
    }

private:
    pthread_t _tid;
    std::string _name;
    uint64_t _time;
    bool _running;

    callback _cb;
};